//
//  ZJPickerview.m
//  EducationWorld
//
//  Created by haoyi on 13-9-25.
//  Copyright (c) 2013年 haoyi. All rights reserved.
//

#import "ZJPickerview.h"
//#import "AppPublic.h"
@implementation ZJPickerview

@synthesize isShowed = _iSShowed;
@synthesize zjDelegate;


-(void)dealloc{

    [_strKeyName release];
    
    [super dealloc];
}

- (id)initWithTitle:(NSString *)title KeyName:(NSString *)keyName{

    self = [super init];
    if (self) {
        _a=1;
        self.frame = CGRectMake(0, 44, fWidth_Current, 260);


        _strKeyName = keyName;
        [_strKeyName retain];
        
        _zjPickView = [[UIPickerView alloc] initWithFrame:
                                  CGRectMake(0, 44, fWidth_Current, 216)];
        
        _zjPickView.dataSource = self;
        _zjPickView.delegate = self;
        _zjPickView.backgroundColor = [UIColor colorWithRed:0.11 green:0.13 blue:0.16 alpha:1.0];
        _zjPickView.showsSelectionIndicator = YES;
        
        [self addSubview:_zjPickView];
        [_zjPickView release];
        
        _pickviewToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, fWidth_Current, 44)];
        
        _pickviewToolbar.barStyle = UIBarStyleBlackOpaque;
		_pickviewToolbar.barTintColor=cellcolor;

		UIBarButtonItem *cancelBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"取消", @"")
																			style:UIBarButtonItemStyleBordered
																		   target:self
																		   action:@selector(cancel222)];
		
		UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																					  target:nil
																					  action:nil];
		
		UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"确定", @"")
																		style:UIBarButtonItemStyleDone
																	   target:self
																	   action:@selector(done222)];
		cancelBarItem.tintColor=[UIColor whiteColor];
        doneBarItem.tintColor=[UIColor whiteColor];

		[_pickviewToolbar setItems:[NSArray arrayWithObjects:cancelBarItem, spaceBarItem, doneBarItem, nil]];
		[cancelBarItem release];
		[spaceBarItem release];
		[doneBarItem release];
		[self addSubview:_pickviewToolbar];
		[_pickviewToolbar release];
        
        _iSShowed = NO;
    }
    
    return self;
}
-(void)settype:(int)a
{
    _a=a;
}
#define kDuration   0.3
#define kFont       20.0
- (void)showInView:(UIView *)view withArray:(NSArray *)array selectedIndex:(int)selectedIndex{

    _contentArray = [array retain];

    if (array.count < 1) {
        return;
    }
    
    blackview=[[UIButton alloc]init];
    blackview.backgroundColor=[UIColor blackColor];
    blackview.frame=CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    blackview.alpha=0.5;
    [blackview addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:blackview];
    [blackview release];
    [_zjPickView reloadAllComponents];
    //默认选中index
    _iSelectedIndex = selectedIndex;
    [_zjPickView selectRow:_iSelectedIndex inComponent:0 animated:NO];
    
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:@"ZJPickerview"];
    
    self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    if (_a==0) {
        self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height+64, self.frame.size.width, self.frame.size.height);
    }
    [view addSubview:self];
    
    _iSShowed = YES;
}
-(void)buttonClicked:(UIButton *)sender{
    
}
-(void)hideZJPickerView{

    if (_iSShowed) {
        
        [self cancel222];
    }
}

#pragma mark - Button lifecycle
- (void)cancel222{
    
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"ZJPickerview"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];

    [_contentArray release];
    [blackview removeFromSuperview];
    _iSShowed = NO;
}

- (void)done222{
    
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"ZJPickerview"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    
    if (self.zjDelegate && [self.zjDelegate respondsToSelector:@selector(ZJPickerview:didFineshesAtIndex:)]) {
        
        [self.zjDelegate ZJPickerview:self didFineshesAtIndex:_iSelectedIndex];
    }
    
    [_contentArray release];
    [blackview removeFromSuperview];
    _iSShowed = NO;
}

#pragma mark - PickerView lifecycle
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return [_contentArray count];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumScaleFactor = 8.;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:1];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:kFont]];
        pickerLabel.textColor=[UIColor whiteColor];
    }
    // Fill the label text here
    if (_strKeyName) {
        
        NSDictionary *tempDict = [_contentArray objectAtIndex:row];
        pickerLabel.text = [tempDict objectForKey:_strKeyName];
    }else{
    
        pickerLabel.text = [_contentArray objectAtIndex:row];
    }
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];

    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    _iSelectedIndex = row;
}


@end
